package com.spic.business.train.certificate.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.train.certificate.domain.UserCertificate;
import com.spic.business.train.certificate.service.IUserCertificateService;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.UUIDUtil;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 员工证书Controller
 * 
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/train/certificate")
public class UserCertificateController extends BaseController
{
    @Autowired
    private IUserCertificateService userCertificateService;

    /**
     * 查询员工证书列表
     */
    @PreAuthorize(hasPermi = "business/train:certificate:list")
    @GetMapping("/list")
    public TableDataInfo list(UserCertificate userCertificate)
    {
        startPage();
        List<UserCertificate> list = userCertificateService.selectUserCertificateList(userCertificate);
        return getDataTable(list);
    }

    /**
     * 导出员工证书列表
     */
    @PreAuthorize(hasPermi = "business/train:certificate:export")
    @Log(title = "员工证书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCertificate userCertificate) throws IOException
    {
        List<UserCertificate> list = userCertificateService.selectUserCertificateList(userCertificate);
        ExcelUtil<UserCertificate> util = new BusinessExcelUtil<UserCertificate>(UserCertificate.class);
        util.exportExcel(response, list, "员工证书数据");
    }

    /**
     * 获取员工证书详细信息
     */
    @PreAuthorize(hasPermi = "business/train:certificate:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(userCertificateService.selectUserCertificateById(id));
    }

    /**
     * 新增员工证书
     */
    @PreAuthorize(hasPermi = "business/train:certificate:add")
    @Log(title = "员工证书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCertificate userCertificate)
    {
        userCertificate.setId(UUIDUtil.createUUID());
        userCertificate.setCreateBy(String.valueOf(SecurityUtils.getUserId()));

        return toAjax(userCertificateService.insertUserCertificate(userCertificate));
    }

    /**
     * 修改员工证书
     */
    @PreAuthorize(hasPermi = "business/train:certificate:edit")
    @Log(title = "员工证书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCertificate userCertificate)
    {
        return toAjax(userCertificateService.updateUserCertificate(userCertificate));
    }

    /**
     * 删除员工证书
     */
    @PreAuthorize(hasPermi = "business/train:certificate:remove")
    @Log(title = "员工证书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(userCertificateService.deleteUserCertificateByIds(ids));
    }
}
